<?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 

// @TODO: Get results only where they are set to active

class Hocvien_model extends CI_Model
{
	protected $_table;	    
	/**
	 * Constructor
	 *
	 * @access	public
	 */
	function __construct()
	{
		parent::__construct();
		$this->load->helper('date');
	}
		
	/**
	 * Get all Categories
	 *
	 * @access	public
	 * @return 	object	Categories
	 */
	function get_all_hocvien($limit, $start)
	{
		$this->db->select('id, mahv, tenhv, ngaysinh, didong, gioitinh, email, cmnd, bodoi, nguyenquan, status');
		$this->db->from('hocvien');
		$this->db->order_by('id','desc');
		$this->db->limit($limit, $start);
		$query = $this->db->get();
		return $query->result_array();
	}	
	
	function get_tenlop()
	{
		$this->db->select('id, malop, tenlop');
		$this->db->from('lophoc');
		$query = $this->db->get();
		return $query->result();
	}
	
	// --------------------------------------------------------------------
	
	/**
	 * Get all entries
	 *
	 * @access	public
	 * @return 	object	Entries
	 */
	
	public function check_mahv()
	{
		$mahv = $this->input->post('mahv');
        $query =  "
			SELECT id, mahv
			FROM hocvien WHERE mahv ='$mahv' 
			";
        $result = $this->db->query($query);
        if($result->num_rows() >= 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function check_cmnd()
	{
        $this->db->where('cmnd',$this->input->post('cmnd'));
        $query = $this->db->get('hocvien');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function check_malop()
	{
        $this->db->where('malop',$this->input->post('malop'));
        $query = $this->db->get('lophoc');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }

	public function check_email()
	{
        $this->db->where('email',$this->input->post('email'));
        $query = $this->db->get('user');
        if($query->num_rows() == 1)
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
	public function get_hocvien_info($id)
	{
		$this->db->select('*');
		$this->db->from('hocvien');
		$this->db->where('id', $id);
		$query = $this->db->get();
		return $query->row();
	}
	
	public function edit_hocvien($id)
	{
		if($this->input->post('bodoi')==0)
		{
			$sothexuatngu = '';
			$ngayxuatngu = ($this->input->post('ngayxuatngu') != '') ? date('Y-m-d', strtotime($this->input->post('ngayxuatngu'))) : date('Y-m-d');
		}
		else
		{
			$sothexuatngu = $this->input->post('sothexuatngu');
			$ngayxuatngu = ($this->input->post('ngayxuatngu') != '') ? date('Y-m-d', strtotime($this->input->post('ngayxuatngu'))) : date('Y-m-d');
		}
		$date = str_replace('/', '-', $this->input->post('ngaysinh'));
		$ngaysinh = date('Y-m-d', strtotime($date));
		
		$date2 = str_replace('/', '-', $this->input->post('ngaysinh'));
		$ngaycap = date('Y-m-d', strtotime($date2));
		
		//echo $create_date; exit();
		//$time = mdate('%dd/%mm/%yyyy %H:%m:%s',time());
		$fields = array(
			'mahv' 			=> $this->input->post('mahv'),
			'tenhv'			=> $this->input->post('tenhv'),
			'email'			=> $this->input->post('email'),
			'didong'		=> $this->input->post('didong'),
			'nguyenquan'	=> $this->input->post('nguyenquan'),
			'thuongtru'		=> $this->input->post('thuongtru'),
			'ngaysinh'		=> $ngaysinh,
			'gioitinh'		=> $this->input->post('gioitinh'),
			'hedaotao'		=> $this->input->post('hedaotao'),
			'cmnd'			=> $this->input->post('cmnd'),
			'ngaycap'		=> $ngaycap,
			'noicap'		=> $this->input->post('noicap'),
			'bodoi'			=> $this->input->post('bodoi'),
			'sothexuatngu'	=> $sothexuatngu,
			'ngayxuatngu'	=> $ngayxuatngu,
			'hocvan'		=> $this->input->post('hocvan'),
			'status'		=> $this->input->post('status'),
		);
		
		
		$this->db->set($fields);
		
		$this->db->where('id', $id);
		$this->db->update('hocvien');
	}
	
	function delete_hocvien($id)
	{
		$fields = array(
			'status' 			=> 0,
			'deleted' 			=> 1,
		);
		$this->db->set($fields);
		$this->db->where('id', $id);
		$this->db->update('hocvien');
	}
	

	
	public function change_status_hocvien($id = null,$status = 0)
	{
		$data = array
				(
					'status'=> $status
				);
		$this->db->where('id',$id);
		$this->db->update('hocvien',$data);		
	}
	
	
	public function search_data($keyword)
	{
		$this->db->select('id, mahv, tenhv, ngaysinh, didong, gioitinh, email, cmnd, bodoi, nguyenquan, status, ngaycap');
		$this->db->from('hocvien');
		$this->db->like('mahv', $keyword);
		$this->db->or_like('cmnd', $keyword);
		$this->db->order_by('id', 'DESC');
		$query = $this->db->get();
			
		if ($query->num_rows() > 0)
		{
			$result = $query->result_array();
			return $result;
		}
	}

	public function count_all_hocvien()
	{
		$this->db->select('id');
		$this->db->from('hocvien');
		return $this->db->count_all_results();
	}
}
// END listings_model.php
/* Location: ./application/models/listings_model.php */
